package com.rimi.bbs.config;

import com.rimi.bbs.handler.MyAuthenctiationFailureHandler;
import com.rimi.bbs.handler.MyAuthenctiationSuccessHandler;
import com.rimi.bbs.service.impl.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 *
 * @author 20526
 *
 * Security框架的核心配置类
 *
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private MyUserDetailsService userDetailsService;

    @Autowired
    private MyAuthenctiationSuccessHandler successHandler;

    @Autowired
    private MyAuthenctiationFailureHandler failureHandler;

    /**
     * @return
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        // 使用 BCrypt 加密
        return new BCryptPasswordEncoder();
    }

    /**
     * @param auth
     * @throws Exception
     * 自定义登录逻辑
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //测试登陆
        auth.userDetailsService(userDetailsService)
            .passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin() //  定义当需要用户登录时候，转到的登录页面。
                .loginPage("/user/login.html")
                .loginProcessingUrl("/login")
                .successHandler(successHandler) //定义成功的响应
                .failureHandler(failureHandler) //定义失败的响应
                .and()
                .authorizeRequests()       // 定义哪些URL需要被保护、哪些不需要被保护
                .antMatchers("/user/username/*","/user/forget/**","/mail/vercode/to/*"
                        ,"/user/login.html","/login","/user/register"
                        ,"/user/reg.html","/index","/","/jie/q/**"
                        ,"/jie/detail/**").permitAll()
                .anyRequest()               // 任何请求,登录后可以访问
                .authenticated()
                .and().csrf().disable();
        //以下这句就可以控制单个用户只能创建一个session，也就只能在服务器登录一次
        http.sessionManagement().maximumSessions(1).sessionRegistry(sessionRegistry()).expiredUrl("/user/login.html");
    }

    @Bean
    public SessionRegistry sessionRegistry(){
            return new SessionRegistryImpl();
    }

    /**
     * @param web
     * @throws Exception
     *  放行静态资源
     */

    @Override
    public void configure(WebSecurity web) throws Exception {
        //解决静态资源被拦截的问题
        web.ignoring().antMatchers("/css/**","/images/**","/layui/**","/mods/**","/favicon.ico");
    }

}
